package com.buka.user.controller;


import com.buka.entity.Result;
import com.buka.entity.StatusCode;
import com.buka.server.pojo.UserControl;
import com.buka.user.service.UserService;
import com.buka.utils.BaseExceptionHandler;
import com.buka.utils.JwtUtils;
import com.github.pagehelper.Page;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@Data
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserService userService;
    @Value(value = "72")
    private Long timeout;
    @Value(value ="itbuka")
    private String signWith;

    @RequestMapping("/findAll")
    public Result<List<UserControl>> FindAll() {
        List<UserControl> all = userService.findAll();
        return new Result<>(true, StatusCode.OK, "查询成功", all);
    }
    /**
     * 条件查询
     * @return
     */
    @PostMapping("/findList")
    public Result findList(@RequestBody UserControl user){
//        if (user.getUsername().equals("")) {
//            user.setUsername(null);
//        }
//        if (null != user.getPhone() && user.getPhone().equals("")) {
//            user.setPhone(null);
//        }
        List<UserControl> list = userService.findList(user);
        return new Result("条件查询成功",list);
    }

    @RequestMapping("/findByPage")
    public Result FindByPage(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer size) {
        Page<UserControl> byPage = userService.findByPage(page, size);
        return new Result<>(true, StatusCode.OK, "分页显示成功", byPage);
    }

    @RequestMapping("/controllerLogo/{id}")
    public Result OpenOrStopBrand(@PathVariable Long id) {
        int a = userService.OpenOrStopBrand(id);
        if (a == 0) {
            return new Result<>(true, StatusCode.OK, "启用成功");
        } else
            return new Result<>(true, StatusCode.OK, "禁用成功");
    }

    @PostMapping("/addAndFile")
    public Result UpdateFile(@RequestBody UserControl userControl, @RequestBody MultipartFile file) {
        int a = userService.addAndFile(userControl, file);
        if (a != 1) {
            return new Result(false, StatusCode.ERROR, "error");
        } else
//        String message = (String) data;
            return new Result<>(true, StatusCode.OK, "插入（文件）成功！");
    }

    @RequestMapping("/delete")
    public Result Delete(@RequestParam Long id){
        int a=userService.delete(id);
        if(a!=1){
            return new Result(false, StatusCode.ERROR, "error");
        }else {
            return new Result<>(true, StatusCode.OK, "删除成功！");
        }
    }

    @RequestMapping("/insert")
    public Result Add(@RequestBody UserControl userControl){
        int a=userService.add(userControl);
        if(a!=1){
            return new Result(false, StatusCode.ERROR, "error");
        }else {
            return new Result<>(true, StatusCode.OK, "插入成功！");
        }
    }
    @PostMapping("/add")
    public Result UpdateFile(@RequestBody UserControl userControl) {
        int a = userService.update(userControl);
        if (a != 1) {
            return new Result(false, StatusCode.ERROR, "error");
        } else
//        String message = (String) data;
            return new Result<>(true, StatusCode.OK, "插入（文件）成功！");
    }

    @PostMapping("/login")
    public Result Login(@RequestBody UserControl userControl){
        int a=userService.login(userControl);
        if(a==-1){
            return new Result(false, StatusCode.ERROR, "没这个用户");
        }else if(a!=1){
            return new Result(false, StatusCode.ERROR, "登陆失败");
        }else {

//            Map<String,String> info = new HashMap<>();
//            info.put("username",userControl.getUsername());
//            String token = JwtUtils.createJWT(String.valueOf(userControl.getId()), userControl.getUsername(), null);
//            info.put("token",token);
//            System.out.println(info);
            UserControl userControl1=new UserControl();
            userControl1.setUsername(userControl.getUsername());
            UserControl userControl2=userService.findList(userControl1).get(0);
            Map<String,Object> objectobjectMap=new HashMap<>();
            objectobjectMap.put("timeout",timeout*60*60*1000);
            objectobjectMap.put("signWith",signWith);
            objectobjectMap.put("username",userControl2.getUsername());
            objectobjectMap.put("phoneNum",userControl2.getPhone());
            String token = JwtUtils.jiami(objectobjectMap);
            return new Result<>(true, StatusCode.OK, "登录成功！",token);
        }
    }
}
